import java.util.Enumeration;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;

public class FileAction {
    
    // The object to be notified when a file system operation has finished

    FileActionInvoker invoker;
    
    public FileAction(FileActionInvoker invoker){
    	this.invoker=invoker;    
    }
    
    // get the content of a directory and send the content to the fileActionInvoker 
    // by callig the method browser.updateDirList    
    
    public synchronized void getDirContent(String currentDir){
    	// Guardamos el directorio en el que estamos
    	UtilRMS.setData(UtilRMS.RMS_RUTA_ACTUAL, currentDir);
		
        Enumeration CurrentDirEnum=null ;
        FileConnection currDirFC=null;
        
        boolean isRoot=false;
        try{
            if(currentDir.equals(Constantes.FILE_ROOT))
            {
                
                CurrentDirEnum = FileSystemRegistry.listRoots();
                isRoot=true;
            }
            else
            {
                
                currDirFC = (FileConnection)Connector.open(Constantes.FILE_URL+currentDir, Connector.READ);
                
                
                if(currDirFC!=null)
                    CurrentDirEnum = currDirFC.list();            
            
            }
            
        }
        catch(SecurityException e){
            //System.out.println("Security exception");
            CurrentDirEnum=null ;
        }
        catch(Exception ex){
        	// Si hay exception es porque no existe ya ese directorio -> volvemos al root
            CurrentDirEnum = FileSystemRegistry.listRoots();
            isRoot=true;
            
            //System.out.println("FileSystemThread.run- Exception:"+ex.toString());
        }
        finally
        {
            try
            {  if(currDirFC!=null)             
                currDirFC.close();
            
            }
            catch(Exception e)
            {
                //System.out.println("FileSystemThread.run Exception "+e.toString());
            }    
            currDirFC=null;
            
            invoker.updateDirList(CurrentDirEnum, isRoot);
        }
    }     
}
